<?php
/**
 * User: @lose
 * Date: 2019-09-20
 * Time: 10:31
 */

namespace Biz\service;

use Biz\model\ShopCategoryClassify as mShopCategoryClassify;
use Doraemon\tools\ImgUrl;

class CategoryInfo
{
    public static function getCategoryInfo(array $categoryIds)
    {

        $redis = new \Hhz\Redis\newCache(\hhz\constant\redis\redisKeys::KEY_B_CACHE_CATEGORY_INFO_);

        list($categoryInfo, $noCacheKeys) = $redis->getCaches($categoryIds);

        if ($noCacheKeys) {
            $mCategoryInfo = mShopCategoryClassify::getCategoryInfo($noCacheKeys);
            $mCategoryInfo = array_column($mCategoryInfo, null, 'id');
            $redis->setCaches($mCategoryInfo);
            $categoryInfo = $categoryInfo + $mCategoryInfo;
        }

        foreach ($categoryInfo as $categoryId => &$info) {
            if ($info['status'] != mShopCategoryClassify::STATUS_NORMAL || !in_array($info['deep'], [2, 3])) {
                unset($categoryInfo[$categoryId]);
            }

            $info['category_img'] = $info['icon'] ? ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $info['icon'],
                ImgUrl::IMAGE_SIZE_OP_ORI) : '';
        }

        return $categoryInfo;
    }

    public static function getAdmCategoryIdByParentId($parentId)
    {
        return mShopCategoryClassify::select('cate_id', [
            'parent_id' => $parentId,
            'status' => mShopCategoryClassify::STATUS_NORMAL,
        ]);
    }
}